<?php
if (!defined("IN_OP_CEN"))
{
    exit("You can't access this file directly....");
}
require_once( "class_3d/CheckMode_3d.php" );
$gametype = intval(getvar("gametype"));
$chkGametype = chkGametype($gametype);
if ($chkGametype == 3)
{
    exit("error");
}
$chk = new CheckMode_3d($curruser['acl'], 3, $gametype);
$chk->checkShow3dPage();
$betname_3d = judge_type($gametype);
unset($chk);
if ($curruser['acl'] != "300")
{
    exit();
}
$lotto_id_3d = intval(getvar("lotto_id_3d"));
$tbl = $g_gametype_3d[$gametype]['en'];
include_once( "centralize/centralize_class/config_cen.php" );
include_once( "centralize/centralize_class/db_cen.php" );
if (isset($_POST['cen_id']))
{
    $cen_arr = $_POST['cen_id'];
}
else if (isset($_GET['cen_id']))
{
    $cen_arr = $_GET['cen_id'];
}
else
{
    $cen_arr = array();
}
$cid = intval(getvar("cid"));
if (!isset($g_cen_info[$cid]))
{
    exit(" error cid ");
}
$dbcen = new DB_CEN($cid);
$cidArr = array();
foreach ($cen_arr as $val)
{
    if (!isset($g_cen_info[$val]))
    {
        exit(" error cid ");
    }
    $cidArr[] = intval($val);
}
if (count($cidArr) < 1)
{
    exit("empty cid");
}
$sql = "SELECT open, round FROM lotto_{$tbl} WHERE id = {$lotto_id_3d} LIMIT 1";
$rs = getdb_cen($cid, $sql);
if ($rs->eof)
{
    exit("error lotto_id - {$lotto_id_3d}, {$cid}");
}
$cenLottoIdSQL = " open = '{$rs->f[0]}' and round = '{$rs->f[1]}' ";
$round_c = $rs->f[1];
$open_c = $rs->f[0];
require( "class_3d/SRate_3d.php" );
require( "class_3d/RateConfig_3d.php" );
require( "class_3d/AutoRateConfig_3d.php" );
$conf = new Config();
$RateConfig_3d = new RateConfig_3d($gametype);
$AutoRateConfig_3d = new AutoRateConfig_3d($betname_3d, $g_autorate_3d, $gametype);
$disk = "a";
$playtype = getvar("playtype");
$order = "0";
$split = "1";
$ratemod = "0";
$bettype = getvar("actionBettype");
$numberStr = getvar("actionNum");
$mp = getvar("actionMP");
$addVal = floatval(getvar("addVal"));
$addValType = intval(getvar("addType"));
$addType1Val = floatval(getvar("addType1Val"));
$method = intval(getvar("method"));
$pagename = intval(getvar("pagename"));
if ($mp == "m")
{
    $addVal = 0 - $addVal;
}
$errorMsg = "";
if ($curruser['enabled'] == 0 || $curruser['enabled'] == 2)
{
    exit();
}
else if ($playtype == "" || !isset($RateConfig_3d->playGroupArr02[$playtype]))
{
    echo "playtype:{$playtype}";
    exit();
}
if ($numberStr == "NORMAL")
{
    $numberStr = "012,013,014,015,016,017,018,019,021,023,024,025,026,027,028,029,031,032,034,035,036,037,038,039,041,042,043,045,046,047,048,049,051,052,053,054,056,057,058,059,061,062,063,064,065,067,068,069,071,072,073,074,075,076,078,079,081,082,083,084,085,086,087,089,091,092,093,094,095,096,097,098,102,103,104,105,106,107,108,109,120,123,124,125,126,127,128,129,130,132,134,135,136,137,138,139,140,142,143,145,146,147,148,149,150,152,153,154,156,157,158,159,160,162,163,164,165,167,168,169,170,172,173,174,175,176,178,179,180,182,183,184,185,186,187,189,190,192,193,194,195,196,197,198,201,203,204,205,206,207,208,209,210,213,214,215,216,217,218,219,230,231,234,235,236,237,238,239,240,241,243,245,246,247,248,249,250,251,253,254,256,257,258,259,260,261,263,264,265,267,268,269,270,271,273,274,275,276,278,279,280,281,283,284,285,286,287,289,290,291,293,294,295,296,297,298,301,302,304,305,306,307,308,309,310,312,314,315,316,317,318,319,320,321,324,325,326,327,328,329,340,341,342,345,346,347,348,349,350,351,352,354,356,357,358,359,360,361,362,364,365,367,368,369,370,371,372,374,375,376,378,379,380,381,382,384,385,386,387,389,390,391,392,394,395,396,397,398,401,402,403,405,406,407,408,409,410,412,413,415,416,417,418,419,420,421,423,425,426,427,428,429,430,431,432,435,436,437,438,439,450,451,452,453,456,457,458,459,460,461,462,463,465,467,468,469,470,471,472,473,475,476,478,479,480,481,482,483,485,486,487,489,490,491,492,493,495,496,497,498,501,502,503,504,506,507,508,509,510,512,513,514,516,517,518,519,520,521,523,524,526,527,528,529,530,531,532,534,536,537,538,539,540,541,542,543,546,547,548,549,560,561,562,563,564,567,568,569,570,571,572,573,574,576,578,579,580,581,582,583,584,586,587,589,590,591,592,593,594,596,597,598,601,602,603,604,605,607,608,609,610,612,613,614,615,617,618,619,620,621,623,624,625,627,628,629,630,631,632,634,635,637,638,639,640,641,642,643,645,647,648,649,650,651,652,653,654,657,658,659,670,671,672,673,674,675,678,679,680,681,682,683,684,685,687,689,690,691,692,693,694,695,697,698,701,702,703,704,705,706,708,709,710,712,713,714,715,716,718,719,720,721,723,724,725,726,728,729,730,731,732,734,735,736,738,739,740,741,742,743,745,746,748,749,750,751,752,753,754,756,758,759,760,761,762,763,764,765,768,769,780,781,782,783,784,785,786,789,790,791,792,793,794,795,796,798,801,802,803,804,805,806,807,809,810,812,813,814,815,816,817,819,820,821,823,824,825,826,827,829,830,831,832,834,835,836,837,839,840,841,842,843,845,846,847,849,850,851,852,853,854,856,857,859,860,861,862,863,864,865,867,869,870,871,872,873,874,875,876,879,890,891,892,893,894,895,896,897,901,902,903,904,905,906,907,908,910,912,913,914,915,916,917,918,920,921,923,924,925,926,927,928,930,931,932,934,935,936,937,938,940,941,942,943,945,946,947,948,950,951,952,953,954,956,957,958,960,961,962,963,964,965,967,968,970,971,972,973,974,975,976,978,980,981,982,983,984,985,986,987";
}
else if (substr($numberStr, 0, 1) == "q")
{
    $tempNumArr = explode("x", substr($numberStr, 1));
    if (count($tempNumArr) != 3)
    {
        header("Cache-Control: no-cache, must-revalidate");
        header("Content-Type: text/plain");
        echo "{\"msg\":\"error\"}";
        exit();
    }
    else
    {
        $allNumTempArr = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
        if (strlen($tempNumArr[0]) < 1)
        {
            $p3arr = $allNumTempArr;
        }
        else
        {
            $p3arr = $RateConfig_3d->mystr_split($tempNumArr[0], 1);
        }
        if (strlen($tempNumArr[1]) < 1)
        {
            $p2arr = $allNumTempArr;
        }
        else
        {
            $p2arr = $RateConfig_3d->mystr_split($tempNumArr[1], 1);
        }
        if (strlen($tempNumArr[2]) < 1)
        {
            $p1arr = $allNumTempArr;
        }
        else
        {
            $p1arr = $RateConfig_3d->mystr_split($tempNumArr[2], 1);
        }
        $numberStr = "";
        foreach ($p3arr as $valp3)
        {
            foreach ($p2arr as $valp2)
            {
                foreach ($p1arr as $valp1)
                {
                    $numberStr .= $valp3 . $valp2 . $valp1 . ",";
                }
            }
        }
        if (substr($numberStr, 0 - 1) == ",")
        {
            $numberStr = substr($numberStr, 0, strlen($numberStr) - 1);
        }
    }
}
$chkBetArr = array("g3" => 24, "g6" => 31);
$chk29BetArr = array("29", "29_0", "29_1", "29_2");
if (isset($chkBetArr[$playtype]) && $method == 2 && !in_array($bettype, $chk29BetArr))
{
    $qicGFlag = true;
    $minBettype = $chkBetArr[$playtype];
    $ratePecFlag = true;
    if ($minBettype == 24)
    {
        $subid = intval(getvar("subid"));
        $bettype = "24_{$subid},25_{$subid},26_{$subid},27_{$subid},28_{$subid}";
    }
    else
    {
        if ($minBettype == 31)
        {
            $subid = intval(getvar("subid"));
            $bettype = "31_{$subid},30_{$subid},32_{$subid},33_{$subid},34_{$subid}";
        }
    }
}
else
{
    $qicGFlag = false;
}
$bettypeArr = explode(",", $bettype);
$numArr = explode(",", $numberStr);
$autoRateArr = $AutoRateConfig_3d->getAutoRateAll();
$disChange = array();
if ($addValType == 0)
{
    $addVal = $addType1Val;
}
$SRate_3d = new SRate_3d($gametype, $cid);
foreach ($cidArr as $cidVal)
{
    $SRate_3d->db->chgCidConnect($cidVal);
    $sql = "select id from lotto_{$tbl} where {$cenLottoIdSQL} ";
    $rs = getdb_cen($cidVal, $sql);
    if ($rs->eof)
    {
        continue;
    }
    $lotto_id = $rs->f[0];
    $fieldArr = array("method" => $method, "gametype" => $gametype, "round" => $round_c, "lotto_id" => $lotto_id, "betcontent" => "", "orate" => "", "nrate" => "", "pagename" => "0", "paperopen" => $open_c);
    $rs->close();
    $okFlag = true;
    $ratePec = 1;
    foreach ($bettypeArr as $valStr)
    {
        $valArr = explode("_", $valStr);
        $val = $valArr[0];
        if (isset($valArr[1]))
        {
            $subid = $valArr[1];
        }
        else
        {
            $subid = 0;
        }
        $valIdx = "{$val}_{$subid}";
        if (!in_array($val, $RateConfig_3d->playGroupArr02[$playtype]))
        {
            $okFlag = false;
            break;
        }
        if (!isset($autoRateArr[$valIdx]) || $autoRateArr[$valIdx]['money'] <= 0)
        {
            array_push($disChange, $val);
            continue;
        }
        switch ($addValType)
        {
            case "0" :
                $addType1ValArr = array("a" => "0", "b" => "0", "c" => "0", "d" => "0");
                break;
            case "1" :
                $tempArr = array(0 => $autoRateArr[$valIdx]['downrate'], 1 => $autoRateArr[$valIdx]['uprate'], 2 => $autoRateArr[$valIdx]['limitrate']);
                $addType1ValArr = array("a" => $tempArr, "b" => $tempArr, "c" => $tempArr, "d" => $tempArr);
                break;
        }
        $rateAChgRange = array();
        $rateArr = $SRate_3d->getRates($lotto_id, 1, $val, $subid);
        if (isset($SRate_3d->betForNumDiffTbl[$val]))
        {
            $numLeng = count($SRate_3d->betForNumDiffTbl[$val]);
            $oldRateArr = $SRate_3d->getRatesNumArr($lotto_id, 1, $val, $subid, $numberStr);
            if ($addValType)
            {
                $ratedata = $RateConfig_3d->my_array_fill_keys($numArr, $addVal * ( 0 - 1 ));
                $SRate_3d->updateRateNumByAddVal($lotto_id, "-1", $val, $ratedata, $subid);
                $newRateArr = $SRate_3d->getRatesNumArr($lotto_id, 1, $val, $subid, $numberStr);
            }
            else
            {
                $ratedata = array();
                foreach ($numArr as $numArrVal)
                {
                    $numTempArr = str_split($numArrVal);
                    $numIdxTemp = count(array_unique($numTempArr));
                    $numIdxTemp = $SRate_3d->betForNumDiffTbl[$val][$numIdxTemp];
                    $ratedata[$numArrVal] = floatval($rateArr[$numIdxTemp] - $addVal);
                }
                $SRate_3d->updateNumRate($lotto_id, 1, $val, $ratedata, $subid);
                $newRateArr = $SRate_3d->getRatesNumArr($lotto_id, 1, $val, $subid, $numberStr);
                $ratedata2 = array();
                foreach ($newRateArr as $newKey => $newVal)
                {
                    $rateDisRange = isset($oldRateArr[$newKey]) ? $newVal - $oldRateArr[$newKey] : $newVal;
                    if ($rateDisRange)
                    {
                        $ratedata2[$newKey] = $rateDisRange * ( 0 - 1 );
                    }
                }
                if (count($ratedata2))
                {
                    $SRate_3d->updateRateNumByAddVal($lotto_id, "2,3,4", $val, $ratedata, $subid);
                }
            }
            foreach ($newRateArr as $newKey => $newVal)
            {
                $rateDisRange = isset($oldRateArr[$newKey]) ? $newVal - $oldRateArr[$newKey] : $newVal;
                $rateAChgRange[] = $rateDisRange;
                if ($rateDisRange)
                {
                    $oldRateTemp = isset($oldRateArr[$newKey]) ? $oldRateArr[$newKey] : 0;
                    $newRateTemp = $newVal;
                    $newKey = str_pad($newKey, $numLeng, "0", STR_PAD_LEFT);
                    $numTempArr = str_split($newKey);
                    $numIdxTemp = count(array_unique($numTempArr));
                    $numIdxTemp = $SRate_3d->betForNumDiffTbl[$val][$numIdxTemp];
                    $fieldArr['betcontent'] = "[{$betname_3d[$valIdx]}]" . $RateConfig_3d->getBetContent($val, $newKey);
                    $fieldArr['orate'] = floatval($rateArr[$numIdxTemp] - $oldRateTemp);
                    $fieldArr['nrate'] = floatval($rateArr[$numIdxTemp] - $newRateTemp);
                    setRateRecord($fieldArr, $cidVal);
                }
            }
        }
        else
        {
            if ($addValType)
            {
                $ratedata = $RateConfig_3d->setRateArrDir($addVal, $numArr);
                $okFlag = $SRate_3d->updateRateByAddVal($lotto_id, "-1", $val, $ratedata[0], $subid);
                $newRateArr = $SRate_3d->getRates($lotto_id, 1, $val, $subid);
            }
            else if ($okFlag = $SRate_3d->updateRate($lotto_id, 1, $val, $ratedata[0], $subid))
            {
                $newRateArr = $SRate_3d->getRates($lotto_id, 1, $val, $subid);
                $ratePecArr = array();
                foreach ($ratedata[0] as $keyIdx => $rateVal)
                {
                    $chkAddVal = $newRateArr[$keyIdx] - $rateArr[$keyIdx];
                    if ($chkAddVal)
                    {
                        $ratePecArr[$keyIdx] = $chkAddVal;
                    }
                }
                if (0 < count($ratePecArr))
                {
                    $okFlag = $SRate_3d->updateRateByAddVal($lotto_id, "2,3,4", $val, $ratePecArr, $subid);
                }
            }
            foreach ($newRateArr as $newKey => $newVal)
            {
                $rateDisRange = $newVal - $rateArr[$newKey];
                $rateAChgRange[] = $rateDisRange;
                if ($rateDisRange)
                {
                    $fieldArr['betcontent'] = "[{$betname_3d[$valIdx]}]" . $RateConfig_3d->getBetContent($val, intval(substr($newKey, 1)));
                    $fieldArr['orate'] = $rateArr[$newKey];
                    $fieldArr['nrate'] = $newVal;
                    setRateRecord($fieldArr, $cidVal);
                }
            }
        }
    }
}
if ($okFlag)
{
    $disChangeStr = 0 < count($disChange) ? "\"" . implode("\",\"", $disChange) . "\"" : "";
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{";
    echo "\"msg\":\"OK\"";
    echo ",\"disChange\":[" . $disChangeStr . "]";
    echo ",\"chgMsg1\":[]";
    echo ",\"chgMsg2\":[]";
    echo "}";
}
else
{
    header("Cache-Control: no-cache, must-revalidate");
    header("Content-Type: text/plain");
    echo "{\"msg\":\"error\"}";
}
exit();
?>